package com.nit.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nit.server.common.QueryPageParam;
import com.nit.server.common.RentApplyParam;
import com.nit.server.common.Result;
import com.nit.server.entity.Rentgoods;
import com.nit.server.entity.Rentrecord;
import com.nit.server.service.RentgoodsService;
import com.nit.server.service.RentrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/rentrecord")
public class RentrecordController {
    @Autowired
    private RentrecordService rentrecordService;
    @Autowired
    private RentgoodsService rentgoodsService;
    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Rentrecord rentrecord){
        return rentrecordService.save(rentrecord)?Result.success():Result.fail();
    }
    //修改
    @PostMapping("/update")
    public Result update(@RequestBody Rentrecord rentrecord){
        return rentrecordService.updateById(rentrecord)?Result.success():Result.fail();
    }
    @PostMapping("/listPage")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result listPage(@RequestBody QueryPageParam pageParam){
        System.out.println(pageParam);
        Page<Rentrecord> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());
        String cId = (String)pageParam.getParams().get("cId");
        String userId = (String)pageParam.getParams().get("userId");
        String rentrecordReturn = (String)pageParam.getParams().get("rentrecordReturn");
        QueryWrapper<Rentrecord> queryWrapper = new QueryWrapper();
        queryWrapper.apply("a.rentgoods_id = b.rentgoods_id and a.user_id = c.user_id ");
        if (StringUtils.isNotBlank(cId)){
            queryWrapper.eq("b.c_id",cId);
        }
        if (StringUtils.isNotBlank(userId)){
            queryWrapper.eq("a.user_id",userId);
        }
        if (StringUtils.isNotBlank(rentrecordReturn)){
            queryWrapper.eq("a.rentrecord_return",rentrecordReturn);
        }
        IPage result = rentrecordService.pageCC(page,queryWrapper);

        return Result.success(result.getTotal(), result.getRecords());
    }
    //归还
    @PostMapping("/rentreturn")
//    public List<User> listPage(@RequestBody HashMap map){
    public Result checkyes(@RequestBody RentApplyParam param){
        System.out.println(param);
        int rentapplyCount = param.getRentapplyCount();
        String rentgoodsId = String.valueOf(param.getRentgoodsId());
        Rentgoods rentgoods = rentgoodsService.getById(rentgoodsId);
        int newnum = rentgoods.getRentgoodsCount()+rentapplyCount;
        rentgoods.setRentgoodsCount(newnum);
        rentgoodsService.updateById(rentgoods);

        int retrecordId = param.getRentrecordId();
        Rentrecord rentrecord = rentrecordService.getById(retrecordId);
        rentrecord.setRentrecordReturn(1);

        return rentrecordService.updateById(rentrecord)?Result.success():Result.fail();
    }
}
